﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jisuanji
{
    public partial class Mainform : Form
    {
        private double firstOperand = 0;
        private string currentOperator = "";
        private string currentInput = "";
        private bool isNewInput = false;

        public Mainform()
        {
            InitializeComponent();
            AttachEventHandlers();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button13_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button15_Click(object sender, EventArgs e)
        {

        }

        private void button11_Click(object sender, EventArgs e)
        {

        }
        private void NumberButton_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            HandleNumberInput(btn.Text);
        }

        private void OperatorButton_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            HandleOperator(btn.Text);
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            CalculateResult();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearAll();
        }

        // === 核心逻辑 ===
        private void HandleNumberInput(string num)
        {
            if (isNewInput)
            {
                txtDisplay.Clear();
                currentInput = "";
                isNewInput = false;
            }

            currentInput += num;
            txtDisplay.Text += num;
        }

        private void HandleOperator(string op)
        {
            if (string.IsNullOrEmpty(currentInput))
            {
                if (txtDisplay.Text.Length > 0 && !isNewInput)
                {
                    // 替换上一个运算符
                    
                    txtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1) + op;
                    currentOperator = op;
                }
                return;
            }

            firstOperand = double.Parse(currentInput);
            currentOperator = op;
            currentInput = "";
            txtDisplay.Text += op;
            isNewInput = false;
        }

        private void CalculateResult()
        {
            if (string.IsNullOrEmpty(currentOperator)) return;

            double secondOperand = double.Parse(currentInput);
            double result = 0;

            switch (currentOperator)
            {
                case "+":
                    result = firstOperand + secondOperand;
                    break;
                case "-":
                    result = firstOperand - secondOperand;
                    break;
                case "*":
                    result = firstOperand * secondOperand;
                    break;
                case "÷":
                    if (secondOperand == 0)
                    {
                        MessageBox.Show("不能除以零！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ClearAll();
                        return;
                    }
                    result = firstOperand / secondOperand;
                    break;
            }

            txtDisplay.Text = $"{firstOperand}{currentOperator}{currentInput}={result}";
            firstOperand = result;
            currentInput = "";
            currentOperator = "";
            isNewInput = true;
        }

        private void ClearAll()
        {
            txtDisplay.Text = "";
            currentInput = "";
            currentOperator = "";
            firstOperand = 0;
            isNewInput = false;
        }

        // === 事件绑定 ===
        private void AttachEventHandlers()
        {
            // 数字按钮
            btn0.Click += NumberButton_Click;
            btn1.Click += NumberButton_Click;
            btn2.Click += NumberButton_Click;
            btn3.Click += NumberButton_Click;
            btn4.Click += NumberButton_Click;
            btn5.Click += NumberButton_Click;
            btn6.Click += NumberButton_Click;
            btn7.Click += NumberButton_Click;
            btn8.Click += NumberButton_Click;
            btn9.Click += NumberButton_Click;

            // 运算符按钮
            btnAdd.Click += OperatorButton_Click;
            btnSubtract.Click += OperatorButton_Click;
            btnMultiply.Click += OperatorButton_Click;
            btnDivide.Click += OperatorButton_Click;

            // 功能按钮
            btnEqual.Click += btnEqual_Click;
            btnClear.Click += btnClear_Click;
        }
    }
}
   

